<?php
/**
 * MageWorx
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the MageWorx EULA that is bundled with
 * this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.mageworx.com/LICENSE-1.0.html
 *
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@mageworx.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade the extension
 * to newer versions in the future. If you wish to customize the extension
 * for your needs please refer to http://www.mageworx.com/ for more information
 * or send an email to sales@mageworx.com
 *
 * @category   MageWorx
 * @package    MageWorx_DeliveryZone
 * @copyright  Copyright (c) 2009 MageWorx (http://www.mageworx.com/)
 * @license    http://www.mageworx.com/LICENSE-1.0.html
 */

/**
 * Delivery Zone extension
 *
 * @category   MageWorx
 * @package    MageWorx_DeliveryZone
 * @author     MageWorx Dev Team <dev@mageworx.com>
 */

class MageWorx_DeliveryZone_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
        $data = $this->getRequest()->getPost('shippingzone', array());
        if (isset($data['country_id'])) {
            $country = Mage::getModel('directory/country')->load($data['country_id']);
            if ($country) {
                if (isset($data['region_id'])) {
                    $region = Mage::getModel('directory/region')->load($data['region_id']);
                    if ($region->getCountryId() != $data['country_id']) {
                        unset($data['region_id']);
                    }
                }
                Mage::helper('deliveryzone')->setLocation($data);
            }
        }
        $this->_redirectReferer();
    }

	protected function _redirectReferer($defaultUrl = null)
	{
	    $refererUrl = $this->_getRefererUrl();
	    if (empty($refererUrl)) {
	        $refererUrl = empty($defaultUrl) ? Mage::app()->getStore()->getBaseUrl() : $defaultUrl;
	    }

	    $this->getResponse()->setRedirect($refererUrl);
	    return $this;
	}

    public function getCheckout()
    {
        return Mage::getSingleton('checkout/session');
    }

    public function getQuote()
    {
        return $this->getCheckout()->getQuote();
    }
}